<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="短信发送" prop="status">
        <el-select v-model="queryParams.status" placeholder="请选择0未发送 1已发送" clearable size="small">
          <el-option
            v-for="dict in statusOptions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['buss:sms:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['buss:sms:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['buss:sms:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['buss:sms:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="smsList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="ID" align="center" prop="id"  :show-overflow-tooltip="true"/>
      <el-table-column label="客户群体" align="center" prop="khqt" :show-overflow-tooltip="true"/>
      <el-table-column label="短信内容" width="280" align="center" prop="dxnr" :show-overflow-tooltip="true"/>
      <el-table-column label="0未发送 1已发送" align="center" prop="status" :formatter="statusFormat" />
      <el-table-column label="支行行长ID" align="center" prop="zhUserId"/>
      <el-table-column label="支行行长姓名" align="center" prop="zhUserName" />
      <el-table-column label="支行行长电话" align="center" prop="zhUserPhone" />
      <el-table-column label="创建时间" align="center" prop="createTime" />
      <el-table-column label="修改时间" align="center" prop="updateTime" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['buss:sms:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['buss:sms:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改短信推送功能对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="短信发送" prop="status">
          <el-select v-model="form.status" placeholder="请选择0未发送 1已发送">
            <el-option
              v-for="dict in statusOptions"
              :key="dict.dictValue"
              :label="dict.dictLabel"
              :value="dict.dictValue"
            ></el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listSms, getSms, delSms, addSms, updateSms } from "@/api/buss/sms";

export default {
  name: "Sms",
  components: {
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 短信推送功能表格数据
      smsList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // ID字典
      idOptions: [],
      // 客户群体字典
      khqtOptions: [],
      // 短信内容字典
      dxnrOptions: [],
      // 0未发送 1已发送字典
      statusOptions: [],
      // 任务ID字典
      taskIdOptions: [],
      // 任务名称字典
      taskNameOptions: [],
      // 支行行长ID字典
      zhUserIdOptions: [],
      // 支行行长姓名字典
      zhUserNameOptions: [],
      // 支行行长电话字典
      zhUserPhoneOptions: [],
      // 支行行长电话字典
      createTimeOptions: [],
      // 支行行长电话字典
      updateTimeOptions: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        id: null,
        khqt: null,
        dxnr: null,
        status: null,
        taskId: null,
        taskName: null,
        zhUserId: null,
        zhUserName: null,
        zhUserPhone: null,
        createTime: null,
        updateTime: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    this.getList();

    this.getDicts("dx_status").then(response => {
      this.statusOptions = response.data;
    });

  },
  methods: {
    /** 查询短信推送功能列表 */
    getList() {
      this.loading = true;
      listSms(this.queryParams).then(response => {
        this.smsList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // ID字典翻译
    idFormat(row, column) {
      return this.selectDictLabel(this.idOptions, row.id);
    },
    // 客户群体字典翻译
    khqtFormat(row, column) {
      return this.selectDictLabel(this.khqtOptions, row.khqt);
    },
    // 短信内容字典翻译
    dxnrFormat(row, column) {
      return this.selectDictLabel(this.dxnrOptions, row.dxnr);
    },
    // 0未发送 1已发送字典翻译
    statusFormat(row, column) {
      return this.selectDictLabel(this.statusOptions, row.status);
    },
    // 任务ID字典翻译
    taskIdFormat(row, column) {
      return this.selectDictLabel(this.taskIdOptions, row.taskId);
    },
    // 任务名称字典翻译
    taskNameFormat(row, column) {
      return this.selectDictLabel(this.taskNameOptions, row.taskName);
    },
    // 支行行长ID字典翻译
    zhUserIdFormat(row, column) {
      return this.selectDictLabel(this.zhUserIdOptions, row.zhUserId);
    },
    // 支行行长姓名字典翻译
    zhUserNameFormat(row, column) {
      return this.selectDictLabel(this.zhUserNameOptions, row.zhUserName);
    },
    // 支行行长电话字典翻译
    zhUserPhoneFormat(row, column) {
      return this.selectDictLabel(this.zhUserPhoneOptions, row.zhUserPhone);
    },
    // 支行行长电话字典翻译
    createTimeFormat(row, column) {
      return this.selectDictLabel(this.createTimeOptions, row.createTime);
    },
    // 支行行长电话字典翻译
    updateTimeFormat(row, column) {
      return this.selectDictLabel(this.updateTimeOptions, row.updateTime);
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        khqt: null,
        dxnr: null,
        status: null,
        taskId: null,
        taskName: null,
        zhUserId: null,
        zhUserName: null,
        zhUserPhone: null,
        createTime: null,
        updateTime: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加短信推送功能";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getSms(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改短信推送功能";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateSms(this.form).then(response => {
              this.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addSms(this.form).then(response => {
              this.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$confirm('是否确认删除短信推送功能编号为"' + ids + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delSms(ids);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('buss/sms/export', {
        ...this.queryParams
      }, `buss_sms.xlsx`)
    }
  }
};
</script>
